{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dd8d8545",
   "metadata": {},
   "source": [
    "## 1. APP 现货交易-市价单开仓函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "533b02e3",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|open_market|市价单购买|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "683d683b",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ccf7b5e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceSPOT\n",
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "10f394fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceSPOT = BinanceSPOT(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "trade = binanceSPOT.trade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef5456cd",
   "metadata": {},
   "source": [
    "## 3. 同步 市价开仓（购买）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "27e295c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cancel_result': None,\n",
      " 'error_result': None,\n",
      " 'func_param': {'callback': None,\n",
      "                'cancel': True,\n",
      "                'delay': 0.2,\n",
      "                'errorback': None,\n",
      "                'meta': {},\n",
      "                'newClientOrderId': '',\n",
      "                'newThread': False,\n",
      "                'openMoney': 6,\n",
      "                'quantity': None,\n",
      "                'symbol': 'MANAUSDT',\n",
      "                'timeout': 60},\n",
      " 'get_order_result': {'code': 200,\n",
      "                      'data': {'clientOrderId': 'lXNgXG3z9VrOpDz0SvO9Pj',\n",
      "                               'cummulativeQuoteQty': '5.66410000',\n",
      "                               'executedQty': '13.00000000',\n",
      "                               'icebergQty': '0.00000000',\n",
      "                               'isWorking': True,\n",
      "                               'orderId': 2207177037,\n",
      "                               'orderListId': -1,\n",
      "                               'origQty': '13.00000000',\n",
      "                               'origQuoteOrderQty': '0.00000000',\n",
      "                               'price': '0.00000000',\n",
      "                               'selfTradePreventionMode': 'EXPIRE_MAKER',\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'stopPrice': '0.00000000',\n",
      "                               'symbol': 'MANAUSDT',\n",
      "                               'time': 1706105326869,\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706105326869,\n",
      "                               'workingTime': 1706105326869},\n",
      "                      'msg': ''},\n",
      " 'meta': {},\n",
      " 'request_param': {'newClientOrderId': '',\n",
      "                   'quantity': '13',\n",
      "                   'side': 'BUY',\n",
      "                   'symbol': 'MANAUSDT',\n",
      "                   'type': 'MARKET'},\n",
      " 'set_order_result': {'code': 200,\n",
      "                      'data': {'clientOrderId': 'lXNgXG3z9VrOpDz0SvO9Pj',\n",
      "                               'cummulativeQuoteQty': '5.66410000',\n",
      "                               'executedQty': '13.00000000',\n",
      "                               'fills': [{'commission': '0.01300000',\n",
      "                                          'commissionAsset': 'MANA',\n",
      "                                          'price': '0.43570000',\n",
      "                                          'qty': '13.00000000',\n",
      "                                          'tradeId': 199517066}],\n",
      "                               'orderId': 2207177037,\n",
      "                               'orderListId': -1,\n",
      "                               'origQty': '13.00000000',\n",
      "                               'price': '0.00000000',\n",
      "                               'selfTradePreventionMode': 'EXPIRE_MAKER',\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'symbol': 'MANAUSDT',\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'transactTime': 1706105326869,\n",
      "                               'type': 'MARKET',\n",
      "                               'workingTime': 1706105326869},\n",
      "                      'msg': ''},\n",
      " 'status': 'FILLED',\n",
      " 'symbol': 'MANAUSDT'}\n"
     ]
    }
   ],
   "source": [
    "open_market3 = trade.open_market(\n",
    "    symbol='MANAUSDT',  # 产品\n",
    "    openMoney=6,  # 开仓金额 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级：quantity > openMoney\n",
    "    # quantity=10,  # 开仓数量\n",
    "    meta={},  # 向回调函数中传递的参数字典\n",
    ")\n",
    "\n",
    "pprint(open_market3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13212b52",
   "metadata": {},
   "source": [
    "## 4. 同步 市价开仓（购买） 回调函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f66869c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行成功回调\n",
    "def callback4(information):\n",
    "    print('callback')\n",
    "    pprint(information)\n",
    "\n",
    "\n",
    "# 执行错误回调\n",
    "def errorback4(information):\n",
    "    print('errorback')\n",
    "    pprint(information)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c7ad44be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "callback\n",
      "{'cancel_result': None,\n",
      " 'error_result': None,\n",
      " 'func_param': {'callback': <function callback4 at 0x7fc479839820>,\n",
      "                'cancel': True,\n",
      "                'delay': 0.2,\n",
      "                'errorback': <function errorback4 at 0x7fc4798398b0>,\n",
      "                'meta': {},\n",
      "                'newClientOrderId': '',\n",
      "                'newThread': False,\n",
      "                'openMoney': 6,\n",
      "                'quantity': None,\n",
      "                'symbol': 'MANAUSDT',\n",
      "                'timeout': 60},\n",
      " 'get_order_result': {'code': 200,\n",
      "                      'data': {'clientOrderId': 'da4TEnGdjUMIdFtpay6Nw6',\n",
      "                               'cummulativeQuoteQty': '5.66670000',\n",
      "                               'executedQty': '13.00000000',\n",
      "                               'icebergQty': '0.00000000',\n",
      "                               'isWorking': True,\n",
      "                               'orderId': 2207177902,\n",
      "                               'orderListId': -1,\n",
      "                               'origQty': '13.00000000',\n",
      "                               'origQuoteOrderQty': '0.00000000',\n",
      "                               'price': '0.00000000',\n",
      "                               'selfTradePreventionMode': 'EXPIRE_MAKER',\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'stopPrice': '0.00000000',\n",
      "                               'symbol': 'MANAUSDT',\n",
      "                               'time': 1706105352138,\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706105352138,\n",
      "                               'workingTime': 1706105352138},\n",
      "                      'msg': ''},\n",
      " 'meta': {},\n",
      " 'request_param': {'newClientOrderId': '',\n",
      "                   'quantity': '13',\n",
      "                   'side': 'BUY',\n",
      "                   'symbol': 'MANAUSDT',\n",
      "                   'type': 'MARKET'},\n",
      " 'set_order_result': {'code': 200,\n",
      "                      'data': {'clientOrderId': 'da4TEnGdjUMIdFtpay6Nw6',\n",
      "                               'cummulativeQuoteQty': '5.66670000',\n",
      "                               'executedQty': '13.00000000',\n",
      "                               'fills': [{'commission': '0.01300000',\n",
      "                                          'commissionAsset': 'MANA',\n",
      "                                          'price': '0.43590000',\n",
      "                                          'qty': '13.00000000',\n",
      "                                          'tradeId': 199517089}],\n",
      "                               'orderId': 2207177902,\n",
      "                               'orderListId': -1,\n",
      "                               'origQty': '13.00000000',\n",
      "                               'price': '0.00000000',\n",
      "                               'selfTradePreventionMode': 'EXPIRE_MAKER',\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'symbol': 'MANAUSDT',\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'transactTime': 1706105352138,\n",
      "                               'type': 'MARKET',\n",
      "                               'workingTime': 1706105352138},\n",
      "                      'msg': ''},\n",
      " 'status': 'FILLED',\n",
      " 'symbol': 'MANAUSDT'}\n"
     ]
    }
   ],
   "source": [
    "open_market4 = trade.open_market(\n",
    "    symbol='MANAUSDT',  # 产品\n",
    "    openMoney=6,  # 开仓金额 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级：quantity > openMoney\n",
    "    # quantity=10,  # 开仓数量\n",
    "    meta={},  # 向回调函数中传递的参数字典\n",
    "\n",
    "    callback=callback4,  # 开仓成功触发的回调函数\n",
    "    errorback=errorback4,  # 开仓失败触发的回调函数\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5307318c",
   "metadata": {},
   "source": [
    "## 5. 异步 市价开仓（购买）回调函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "59a6807b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行成功回调\n",
    "def callback5(information):\n",
    "    print('thread callback')\n",
    "    pprint(information)\n",
    "\n",
    "\n",
    "# 执行错误回调\n",
    "def errorback5(information):\n",
    "    print('thread errorback')\n",
    "    pprint(information)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "919d4df1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Thread(Thread-5, started 123145550364672)>\n",
      "------------------------------\n",
      "thread callback\n",
      "{'cancel_result': None,\n",
      " 'error_result': None,\n",
      " 'func_param': {'callback': <function callback5 at 0x7ff8f0e3e940>,\n",
      "                'cancel': True,\n",
      "                'delay': 0.2,\n",
      "                'errorback': <function errorback5 at 0x7ff8f0e58430>,\n",
      "                'meta': {},\n",
      "                'newClientOrderId': '',\n",
      "                'newThread': True,\n",
      "                'openMoney': None,\n",
      "                'quantity': 15,\n",
      "                'symbol': 'MANAUSDT',\n",
      "                'timeout': 60},\n",
      " 'get_order_result': {'code': 200,\n",
      "                      'data': {'clientOrderId': 'Eud32WLIJbpkrBBLdgnPPf',\n",
      "                               'cummulativeQuoteQty': '6.53250000',\n",
      "                               'executedQty': '15.00000000',\n",
      "                               'icebergQty': '0.00000000',\n",
      "                               'isWorking': True,\n",
      "                               'orderId': 2207180344,\n",
      "                               'orderListId': -1,\n",
      "                               'origQty': '15.00000000',\n",
      "                               'origQuoteOrderQty': '0.00000000',\n",
      "                               'price': '0.00000000',\n",
      "                               'selfTradePreventionMode': 'EXPIRE_MAKER',\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'stopPrice': '0.00000000',\n",
      "                               'symbol': 'MANAUSDT',\n",
      "                               'time': 1706105471010,\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706105471010,\n",
      "                               'workingTime': 1706105471010},\n",
      "                      'msg': ''},\n",
      " 'meta': {},\n",
      " 'request_param': {'newClientOrderId': '',\n",
      "                   'quantity': '15',\n",
      "                   'side': 'BUY',\n",
      "                   'symbol': 'MANAUSDT',\n",
      "                   'type': 'MARKET'},\n",
      " 'set_order_result': {'code': 200,\n",
      "                      'data': {'clientOrderId': 'Eud32WLIJbpkrBBLdgnPPf',\n",
      "                               'cummulativeQuoteQty': '6.53250000',\n",
      "                               'executedQty': '15.00000000',\n",
      "                               'fills': [{'commission': '0.01500000',\n",
      "                                          'commissionAsset': 'MANA',\n",
      "                                          'price': '0.43550000',\n",
      "                                          'qty': '15.00000000',\n",
      "                                          'tradeId': 199517126}],\n",
      "                               'orderId': 2207180344,\n",
      "                               'orderListId': -1,\n",
      "                               'origQty': '15.00000000',\n",
      "                               'price': '0.00000000',\n",
      "                               'selfTradePreventionMode': 'EXPIRE_MAKER',\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'symbol': 'MANAUSDT',\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'transactTime': 1706105471010,\n",
      "                               'type': 'MARKET',\n",
      "                               'workingTime': 1706105471010},\n",
      "                      'msg': ''},\n",
      " 'status': 'FILLED',\n",
      " 'symbol': 'MANAUSDT'}\n"
     ]
    }
   ],
   "source": [
    "# 设置newThread=True\n",
    "open_market5 = trade.open_market(\n",
    "    symbol='MANAUSDT',  # 产品\n",
    "    # openMoney=6, # 开仓金额 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级：quantity > openMoney\n",
    "    quantity=15,  # 开仓数量\n",
    "    callback=callback5,  # 开仓成功触发的回调函数\n",
    "    errorback=errorback5,  # 开仓失败触发的回调函数\n",
    "    meta={},  # 向回调函数中传递的参数字典\n",
    "\n",
    "    newThread=True,  # 是否开启一个新的线程维护这个订单\n",
    ")\n",
    "\n",
    "print(open_market5)\n",
    "print('-' * 30)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
